/*
 ******************************************************************************
 * The contents of this file are subject to the   Compiere License  Version 1.1
 * ("License"); You may not use this file except in compliance with the License
 * You may obtain a copy of the License at http://www.compiere.org/license.html
 * Software distributed under the License is distributed on an  "AS IS"  basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * The Original Code is                  Compiere  ERP & CRM  Business Solution
 * The Initial Developer of the Original Code is Jorg Janke  and ComPiere, Inc.
 * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
 * created by ComPiere are Copyright (C) ComPiere, Inc.;   All Rights Reserved.
 * Contributor(s): Openbravo SLU
 * Contributions are Copyright (C) 2001-2012 Openbravo S.L.U.
 ******************************************************************************
 */
package org.openbravo.erpCommon.ad_forms;

import java.math.BigDecimal;
import javax.servlet.ServletException;
import org.apache.log4j.Logger;
import org.openbravo.database.ConnectionProvider;

public class ProductInfo {
  static Logger log4jProductInfo = Logger.getLogger(ProductInfo.class);

  /**
   * Constructor
   */
  public ProductInfo(String M_Product_ID, ConnectionProvider conn) {
    init(M_Product_ID, conn);
  } // ProductInfo

  public static final BigDecimal ZERO = new BigDecimal("0");
  /** The Product Key */
  public String m_M_Product_ID = "";
  // Product Info
  public String m_AD_Client_ID = "";
  public String m_AD_Org_ID = "";

  public String m_productType = "";
  public String m_ProductCategory = "";

  public String m_C_UOM_ID = "";
  public String m_qty = "0";

  /**
   * Get Product Info (Service, Revenue Recognition). automatically called by constructor
   * 
   * @param M_Product_ID
   *          Product
   */
  private void init(String M_Product_ID, ConnectionProvider conn) {
    m_M_Product_ID = M_Product_ID;
    if (m_M_Product_ID != null && m_M_Product_ID.equals(""))
      return;

    ProductInfoData[] data = null;
    try {
      data = ProductInfoData.select(conn, m_M_Product_ID);
      if (data.length == 1) {
        m_productType = data[0].producttype;
        m_ProductCategory = data[0].value;
        m_C_UOM_ID = data[0].cUomId;
        // reference
        m_AD_Client_ID = data[0].adClientId;
        m_AD_Org_ID = data[0].adOrgId;
      }
    } catch (ServletException e) {
      log4jProductInfo.warn(e);
    }
  } // init

  /**
   * Set Quantity in UOM
   * 
   * @param qty
   *          quantity
   * @param C_UOM_ID
   *          UOM
   */
  public void setQty(String qty, String C_UOM_ID, ConnectionProvider conn) {
    m_qty = getConvertedQty(qty, C_UOM_ID, m_C_UOM_ID, "Y", conn); // StdPrecision
    if (qty != null && m_qty == null) { // conversion error
      log4jProductInfo.warn("setQty - conversion error - set to " + qty);
      m_qty = qty;
    }
  } // setQty

  /**
   * Get Converted Qty
   * 
   * @param qty
   *          The quantity to be converted
   * @param C_UOM_From_ID
   *          The C_UOM_ID of the qty
   * @param C_UOM_To_ID
   *          The targeted UOM
   * @param StdPrecision
   *          if true, standard precision, if false costing precision
   * @return amount
   * @deprecated should not be used
   */
  public static String getConvertedQty(String qty, String C_UOM_From_ID, String C_UOM_To_ID,
      String StdPrecision, ConnectionProvider conn) {
    // Nothing to do
    if (qty.equals("") || (new BigDecimal(qty).compareTo(BigDecimal.ZERO) == 0)
        || C_UOM_From_ID.equals(C_UOM_To_ID))
      return qty;
    //
    String retValue = "";
    ProductInfoData[] data = null;
    try {
      data = ProductInfoData.UOMConvert(conn, qty, C_UOM_From_ID, C_UOM_To_ID, StdPrecision);
    } catch (ServletException e) {
      log4jProductInfo.warn(e);
      return null;
    }
    retValue = data[0].converted;
    return retValue;
  } // getConvertedQty


  /** Product Revenue Acct */
  public static final String ACCTTYPE_P_Revenue = "1";
  /** Product Expense Acct */
  public static final String ACCTTYPE_P_Expense = "2";
  /** Product Asset Acct */
  public static final String ACCTTYPE_P_Asset = "3";
  /** Product COGS Acct */
  public static final String ACCTTYPE_P_Cogs = "4";
  /** Purchase Price Variance */
  public static final String ACCTTYPE_P_PPV = "5";
  /** Invoice Price Variance */
  public static final String ACCTTYPE_P_IPV = "6";
  /** Trade Discount Revenue */
  public static final String ACCTTYPE_P_TDiscountRec = "7";
  /** Trade Discount Costs */
  public static final String ACCTTYPE_P_TDiscountGrant = "8";
  /** Product Revenue Return Acct */
  public static final String ACCTTYPE_P_RevenueReturn = "9";
  /** Product COGS Return Acct */
  public static final String ACCTTYPE_P_CogsReturn = "10";
  /** Product COGS Return Acct */
  public static final String ACCTTYPE_P_DefRevenue = "11";
  /** Product Def Expense Acct */
  public static final String ACCTTYPE_P_DefExpense = "12";

} // ProductInfo
